package monitor

import (
	consts2 "MoSkeleton/config/consts"
	"MoSkeleton/framework"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/services"
	"MoSkeleton/web/pogos/request/monitor"
	"MoSkeleton/web/v1/http/base"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strings"
	"time"
)

type SysOperLogCtrl struct {
	base.BaseCtrl
}

func (s *SysOperLogCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleSysOperLogMsg)
	return msg
}

// GetOperLogPage 获取操作日志列表
// @Tags 操作日志管理
// @Summary 获取操作日志列表
// @Security ApiKeyAuth
// @Param data query monitor.SysOperLogPageReq true "分页查询参数"
// @Success 200 {object} response.Response{data=response.PageResult} "{"code":200,"data":{},"msg":"获取操作日志列表成功"}"
// @Failure 500 {object} response.Response{data=response.PageResult} "{"code":500,"data":{},"msg":"获取操作日志列表失败"}"
// @Router /monitor/operlog/list [get]
func (o *SysOperLogCtrl) GetOperLogPage(c *gin.Context) {
	operLogReq := monitor.SysOperLogPageReq{}
	if err := c.ShouldBind(&operLogReq); err != nil {
		framework.MoLogger.Error("获取操作日志列表失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_CommonSelectListFailMsg)
		return
	}
	operLogReq.ShouldBindParams(c)
	operLogPage, err := services.ServiceGroupInst.SystemGroupInst.SysOperLogServiceInst.GetOperLogPage(&operLogReq)
	if err != nil {
		framework.MoLogger.Error("获取操作日志列表失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_CommonSelectListFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommonGetListSuccessMsg, operLogPage)
}

// DeleteOperLogs 删除操作日志
// @Tags 操作日志管理
// @Summary 删除操作日志
// @Security ApiKeyAuth
// @Param data path string true "操作日志ID"
// @Success 200 {object} response.Response{msg=string} "{"code":200,"data":{},"msg":"删除操作日志成功"}"
// @Failure 500 {object} response.Response{msg=string} "{"code":500,"data":{},"msg":"删除操作日志失败"}"
// @Router /monitor/operlog/{ids} [delete]
func (o *SysOperLogCtrl) DeleteOperLogs(c *gin.Context) {
	operLogIdStr := c.Param("ids")
	if operLogIdStr == "" {
		framework.MoLogger.Error("参数错误")
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	idStrs := strings.Split(operLogIdStr, ",")
	ids := utils.StrsToUint64s(&idStrs)
	ok, err := services.ServiceGroupInst.SystemGroupInst.SysOperLogServiceInst.DeleteOperLogs(ids)
	if err != nil {
		framework.MoLogger.Error("删除操作日志失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_CommDeleteFailMsg)
		return
	}
	if !ok {
		framework.MoLogger.Error("删除操作日志失败")
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_CommDeleteFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommDeleteSuccessMsg, nil)
}

// CleanTable 清空操作日志
// @Tags 操作日志管理
// @Summary 清空操作日志
// @Security ApiKeyAuth
// @Success 200 {object} response.Response{msg=string} "{"code":200,"data":{},"msg":"清空操作日志成功"}"
// @Failure 500 {object} response.Response{msg=string} "{"code":500,"data":{},"msg":"清空操作日志失败"}"
// @Router /monitor/operlog/clean [delete]
func (o *SysOperLogCtrl) CleanTable(c *gin.Context) {
	err := services.ServiceGroupInst.SystemGroupInst.SysOperLogServiceInst.CleanTable()
	if err != nil {
		framework.MoLogger.Error("清空操作日志失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_OperLogClearFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_OperLogClearSuccessMsg, nil)
}

// ExportOperLogs 导出操作日志
// @Tags 操作日志管理
// @Summary 导出操作日志
// @Security ApiKeyAuth
// @Param data query monitor.SysOperLogPageReq true "分页查询参数"
// @Success 200 {object} response.Response{msg=string} "{"code":200,"data":{},"msg":"导出操作日志成功"}"
// @Failure 500 {object} response.Response{msg=string} "{"code":500,"data":{},"msg":"导出操作日志失败"}"
// @Router /monitor/operlog/export [POST]
func (o *SysOperLogCtrl) ExportOperLogs(c *gin.Context) {
	o.IgnoreLogResponseData(c)
	operLogReq := monitor.SysOperLogPageReq{}
	if err := c.ShouldBind(&operLogReq); err != nil {
		framework.MoLogger.Error("导出操作日志失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_OperLogExportFailMsg)
		return
	}
	buf, err := services.ServiceGroupInst.SystemGroupInst.SysOperLogServiceInst.ExportOperLogs(&operLogReq)
	if err != nil {
		framework.MoLogger.Error("导出操作日志失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_OperLogExportFailMsg)
		return
	}
	response.SuccessWithFileData(c, fmt.Sprintf(consts2.Excel_Export_OperLog, time.Now().Unix()), buf)
}
